$icon-size: 1.3rem;

// Search button in navbar
.search-button {
  background: none;
  border: none;
  padding: 2px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-left: 0.25em; // Match your other nav items' margin

  img {
    border: none;
  }

  .search-icon {
    width: 16px; // Match your theme button size
    height: 16px;
  }

  &:hover {
    background-color: transparent;
  }
}

// Theme-specific styles for search icon
html.dark .search-button .search-icon {
  filter: invert(1);
}

html.light .search-button .search-icon {
  filter: invert(0);
}

// Search modal
.search-modal {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1000;
  background: rgba(0, 0, 0, 0.2);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);

  #modal-content {
    position: relative;
    margin: 8% auto;
    width: 80%;
    max-width: 28rem;
    background-color: var(--bg-0);
    border: 1px solid var(--bg-1);
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  }

  #searchBar {
    display: flex;
    align-items: center;
    padding: 1rem;
    gap: 0.5rem;

    #searchInput {
      flex: 1;
      padding: 0.75rem 2.5rem;
      font-size: 1rem;
      color: var(--text-0);
      background: var(--bg-1);
      border: 1px solid var(--bg-1);
      border-radius: 20px;
      width: 100%;

      &:focus {
        outline: none;
        border-color: var(--primary-color);
      }

      &::placeholder {
        color: var(--text-1);
      }
    }

    .close-icon {
      position: absolute;
      right: 1.5rem;
      display: none;
      padding: 4px;
      cursor: pointer;

      svg {
        width: $icon-size;
        height: $icon-size;
        fill: var(--text-1);
      }
    }
  }

  #results-container {
    display: none;
    border-top: 1px solid var(--bg-1);

    #results-info {
      padding: 0.5rem;
      color: var(--text-1);
      font-size: 0.8rem;
      text-align: center;
    }

    #results {
      max-height: 50vh;
      overflow-y: auto;

      > div {
        padding: 0.75rem 1rem;
        cursor: pointer;

        &[aria-selected="true"] {
          background: var(--primary-color);

          * {
            color: var(--hover-color) !important;
          }
        }

        span:first-child {
          display: block;
          color: var(--text-0);
          font-weight: 500;
          margin-bottom: 0.25rem;
        }

        span:nth-child(2) {
          display: block;
          color: var(--text-1);
          font-size: 0.9rem;
        }

        &:hover:not([aria-selected="true"]) {
          background: var(--bg-1);
        }
      }
    }
  }

  #modal-content {
    position: relative;
    margin: 8% auto;
    width: 80%;
    max-width: 28rem;
    background-color: var(--bg-0);
    border: 1px solid var(--bg-1);
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    padding: 1rem;

    h1 {
      margin-top: 0; // Override the default 2em margin
      margin-bottom: 1rem;
      font-size: 1.2rem;

      &::before {
        color: var(--primary-color);
        content: "# ";
      }
    }
  }
}

#searchBar {
  position: relative;
  display: flex;
  align-items: center;
  padding: 1rem;

  // Clear button styling
  .clear-button {
    position: absolute;
    right: 1.5rem;
    background: none;
    border: none;
    padding: 4px;
    cursor: pointer;
    display: none; // Initially hidden, shown via JS when input has text
    width: 24px;
    height: 24px;

    svg {
      width: 100%;
      height: 100%;
      fill: var(--text-1); // Use your theme text color
    }

    &:hover {
      svg {
        fill: var(--primary-color);
      }
    }
  }

  // Make sure input accommodates the clear button
  #searchInput {
    padding-right: 2.5rem; // Give space for the clear button
  }
}

// Mobile adjustments
@media only screen and (max-width: 600px) {
  .search-modal {
    #modal-content {
      margin: 4% auto;
      width: 92%;
    }

    #results {
      max-height: 70vh;
    }
  }
}
